﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WorkModule.ServiceReference1;

namespace WorkModule.Views
{
    /// <summary>
    /// Interaction logic for Test1ModuleMainView.xaml
    /// </summary>
    /// 

     [Export("Test1ModuleMainView")]
    public partial class Test1ModuleMainView : UserControl
    {
        ServiceReference1.Container context = 
            new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));

        public Test1ModuleMainView()
        {
            InitializeComponent();

            combo();

            combo2();

            dataload();

            txtEnable();

           
            
            //Lot_ProductCombo.SelectionChanged += (sender, e) => ;

        }


        public void combo()
        {
            var pp = context.productplan_put.Where(kkk=>kkk.productplan_code == "R").Select(kkk => kkk);


            //Lot_ProductCombo.ItemsSource = pp;
            //Lot_ProductCombo.DisplayMemberPath = "work_order_number";
            //Lot_ProductCombo.DisplayMemberPath = "productplan";

            foreach (var kk in pp)
            {
                Lot_ProductCombo.Items.Add(kk.productplan);
            }



            for (int i = 0; i < Lot_ProductCombo.Items.Count; i++)
            {
                for (int y = 0; y < Lot_ProductCombo.Items.Count; y++)
                {
                    if (y != i && Lot_ProductCombo.Items[i].ToString() == Lot_ProductCombo.Items[y].ToString())
                    {
                        Lot_ProductCombo.Items.RemoveAt(i);
                        break;
                    }
                }
            }

            

            //Lot_ProductCombo.SelectionChanged += (sender, e) =>
        }

        public void combo2()
        {
            Lot_ProductCombo.SelectionChanged += (sender, e) =>
            {
                lotnoCombo.Items.Clear();

                var pp = context.productplan_put.Where(kk => kk.productplan == Lot_ProductCombo.SelectedItem.ToString()).Select(kkk => kkk);


                //lotnoCombo.ItemsSource = pp;
                //lotnoCombo.DisplayMemberPath = "work_order_number";

                foreach (var kk in pp)
                {
                    lotnoCombo.Items.Add(kk.work_order_number);
                }

                for (int i = 0; i < lotnoCombo.Items.Count; i++)
                {
                    for (int y = 0; y < lotnoCombo.Items.Count; y++)
                    {
                        if (y != i && lotnoCombo.Items[i].ToString() == lotnoCombo.Items[y].ToString())
                        {
                            lotnoCombo.Items.RemoveAt(i);
                            break;
                        }
                    }
                }

                lotnoCombo.IsEnabled = true;
                txtqun.Text = "";
                txtlocation.Text = "";
               
                
            };

        }

        public void dataload()
        {
            lotnoCombo.SelectionChanged += (sender, e) =>
            {
                try
                {
                    var pp = context.productplan_put.Where(kkk => kkk.productplan == Lot_ProductCombo.SelectedItem.ToString() &
                                                                                             kkk.work_order_number == Convert.ToInt32(lotnoCombo.SelectedItem.ToString())).SingleOrDefault();


                    

                    txtqun.Text = pp.product_quantity.ToString();
                    txtlocation.Text = pp.location;

                    txtSubCom.IsEnabled = true;
                    txtSubCom.Text = "";
                    txtAllCom.Text = "";
                    txtAllq.Text = "";
                    txtSetCom.Text = "";
                    txtSetq.Text = "";
                }
                catch(TargetInvocationException Err1)
                {
                    
                }
             

            };





        }

        public void txtEnable()
        {
            txtSubCom.TextChanged += (sender, e) => btnSubCom.IsEnabled = true;
            txtSetCom.TextChanged += (sender, e) => btnSetCom.IsEnabled = true;
            txtAllCom.TextChanged += (sender, e) => Complete();
        }

        private void btnSubCom_Click(object sender, RoutedEventArgs e)
        {
           var Com = (Convert.ToInt32(txtqun.Text) - Convert.ToInt32(txtSubCom.Text)).ToString();

           txtqun.Text = Com;

            if (txtSetq.Text == "")
            {
                txtSetq.Text = txtSubCom.Text;
            }
            else
            {
                txtSetq.Text = (Convert.ToInt32(txtSetq.Text) + Convert.ToInt32(txtSubCom.Text)).ToString();

            }


           

            var WorkID = context.Works.ToList().Count();

            Work InsertWork = new Work
            {
                id = WorkID,
                work_order_number = this.lotnoCombo.SelectedItem.ToString(),
                work_place = txtlocation.Text,
                work_sub_ing = Convert.ToInt32(txtqun.Text),
                work_sub_complete = Convert.ToInt32(txtSubCom.Text),
                work_set_ing = 0,
                work_set_complete = 0
            };

            context.AddToWorks(InsertWork);

            txtSetCom.IsEnabled = true;

            txtSubCom.Text = "";


        }

        private void btnSetCom_Click(object sender, RoutedEventArgs e)
        {
           var Com = (Convert.ToInt32(txtSetq.Text) - Convert.ToInt32(txtSetCom.Text)).ToString();

            txtSetq.Text = Com;

            if (txtAllq.Text == "")
            {
                txtAllq.Text = txtSetCom.Text;
            }
            else
            {
                txtAllq.Text = (Convert.ToInt32(txtAllq.Text) + Convert.ToInt32(txtSetCom.Text)).ToString();

            }


            txtAllCom.IsEnabled = true;

            txtSetCom.Text = "";

            
        }


        private void btnAllCom_Click(object sender, RoutedEventArgs e)
        {
            btnComplete.IsEnabled = true;
        }

        public void Complete()
        {

            var pp = context.productplan_put.Where(kkk => kkk.productplan == Lot_ProductCombo.SelectedItem.ToString() &
                                                                                              kkk.work_order_number == Convert.ToInt32(lotnoCombo.SelectedItem.ToString())).SingleOrDefault();

           if(txtAllCom.Text == pp.product_quantity.ToString())
            {
                btnAllCom.IsEnabled = true;
            }
           else
            {
                btnAllCom.IsEnabled = false;
            }

        }

        private void btnComplete_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("제품이 완성 되었습니다.", "완료 메세지");

           
            txtSetCom.IsEnabled = false;
            txtAllCom.IsEnabled = false;
            txtSubCom.IsEnabled = false;
            txtqun.Text = "";
            txtSubCom.Text = "";
            txtAllCom.Text = "";
            txtAllq.Text = "";
            txtSetCom.Text = "";
            txtSetq.Text = "";
            

            btnSubCom.IsEnabled = false;
            btnSetCom.IsEnabled = false;
            btnComplete.IsEnabled = false;
            btnAllCom.IsEnabled = false;

            lotnoCombo.SelectedIndex = -1;
          

            txtEnable();
        }
    }
}
